package com.lp.compt.services;

import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import com.lp.compt.repositories.SysUserEntityRepository;
import com.lp.compt.utility.SFWhere;
import com.lp.compt.pojo.SysUserEntity;

import java.util.Calendar;
import java.util.Map;
import java.util.Optional;

/**
 * 服务类
 *
 * @author
 * @since 1.0.0
 */
@Service
public class SysUserEntityService {
    @Autowired
    SysUserEntityRepository sysUserEntityRepository;

    /**
     * 根据SysUserEntity的字段自动生成条件,字段值为null不生成条件
     * 如果是数值型的字段,前端不传入值,默认是0,例如ID的类型是Long,如果不传值,默认是0
     * 可以自己设置下SFWhere.and(sysUserEntity).equal(实体.getId()>0,"id",实体.getId()).build()
     *
     * @param sysUserEntity 实体对象
     * @param pageable      分页对象
     * @return 返回分页\状态码
     */
    public ResponseEntity<Object> search(SysUserEntity sysUserEntity, Pageable pageable) {
        Page<SysUserEntity> all = sysUserEntityRepository.findAll(SFWhere.and(sysUserEntity)
                //.equal(user.getId() > 0, "id", user.getId())
                //.in(true, "userName", longs)
                //.equal(!字段值的判断.equals("") && 字段值的判断 != null, "字段名称", 字段值)
                //.like(字段值的判断 != null, "字段名称", "%" + 字段值 + "%")
                //....
                .build(), pageable);
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    public SysUserEntity login(String phone, String password) {
        Optional<SysUserEntity> optional = sysUserEntityRepository.findOne(Example.of(SysUserEntity.builder().name(phone).build()));
        if (!optional.isPresent()) {
            SysUserEntity entity = SysUserEntity.builder()
                    .admin(0)
                    .status(1)
                    .createTime(Calendar.getInstance().getTime())
                    .updateTime(Calendar.getInstance().getTime())
                    .name(phone)
                    .password(password)
                    .build();
            sysUserEntityRepository.save(entity);
            return entity;
        }
        SysUserEntity user = optional.get();
        if (password.equals(user.getPassword())) {
            return user;
        }
        return null;
    }

    public void info(Map<String, String> map) {
        SysUserEntity entity = sysUserEntityRepository.getOne(Integer.valueOf(map.get("id")));
        entity.setMail(map.get("mial"));
        entity.setSex(map.get("sex"));
        entity.setMajor(map.get("major"));
        entity.setSchool(map.get("school"));
        entity.setUsername(map.get("username"));
        entity.setMail(map.get("mail"));
        sysUserEntityRepository.save(entity);
    }

    public SysUserEntity getInfo(Integer id) {
        SysUserEntity entity = sysUserEntityRepository.getOne(id);
        return entity;
    }
}
